#!/bin/bash
#
# 
#
# sets up directory structure and some starter files for a new Zend Framework application.
# first chdir into the dir under which you want to install the app files, then run me.
# 
#

if [ $# -ne 1 ]; then
    echo "usage: $0 app_name" 1>&2
    exit 1
fi

if [ ! -w . ]; then
    echo "you don't have write permission to `pwd`."
    exit 1;
fi

mkdir $1
cd $1

# make directories
mkdir controllers models views

# write an IndexController class
cat <<EOF >controllers/IndexController.php
<?php
require_once 'Zend/Controller/Action.php';

class IndexController extends Zend_Controller_Action
{
    public function indexAction()
    {
    }
}
EOF

# make more directories
mkdir views/scripts
mkdir views/scripts/index
mkdir views/scripts/error
mkdir views/helpers
mkdir views/filters


# write out an index.phtml view for indexAction()

cat <<EOF >views/scripts/index/index.phtml
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>My first Zend Framework App</title>
</head>
<body>
    <h1>Hello, World!</h1>
    <p>We are ready to rock.</p>
</body>
</html>
EOF

# write an ErrorController

cat <<EOF >controllers/ErrorController.php
<?php
/** Zend_Controller_Action */
require_once 'Zend/Controller/Action.php';

class ErrorController extends Zend_Controller_Action
{
    public function errorAction()
    {
    }
}

EOF

# write an ErrorController class

cat <<EOF >controllers/ErrorController.php

<?php
/** Zend_Controller_Action */
require_once 'Zend/Controller/Action.php';

class ErrorController extends Zend_Controller_Action
{
    public function errorAction()
    {
    }
}

EOF


cat <<EOF >views/scripts/error/error.phtml

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Error</title>
</head>
<body>
    <h1>An error occurred</h1>
    <p>An error occurred; please try again later.</p>
</body>
</html>
EOF


# write a bootstrap file

CONTROLLER_PATH=`pwd`/controllers

cat <<EOF >index.php

<?php
require_once 'Zend/Controller/Front.php';
Zend_Controller_Front::run('$CONTROLLER_PATH');
EOF

# ... and a .htaccess

cat <<EOF >.htaccess
RewriteEngine on
RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php
EOF

# move both index.ph and .htaccess to the web root dir

echo -n "Enter the full filesystem path to your web doc root (w/o trailing slash): ";
read WEB_ROOT

if mv .htaccess index.php $WEB_ROOT/ ;
then
    echo "now just hit http://yoursite/ and see if it works!"
else
    exit 1
fi;
exit 0;